﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace UrlRouter.Web
{
    public class UrlHelper
    {
        private string applicationPath;
        /// <summary>
        /// 类初始化
        /// </summary>
        public UrlHelper()
        {
            this.applicationPath = (HttpRuntime.AppDomainAppVirtualPath.Length > 1) ? HttpRuntime.AppDomainAppVirtualPath : String.Empty;

        }
        /// <summary>
        /// 重写设定url加上根路径符 移除~符号
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string HandleRootOperator(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                if (url.StartsWith("^~/"))
                    return "^" + this.applicationPath + url.Substring(2);
                else if (url.StartsWith("~/"))
                    return this.applicationPath + url.Substring(1);
            }
            return url;
        }
        /// <summary>
        /// 移除url的^~符号
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string RemoveHandleRoot(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                if (url.StartsWith("^~/"))
                    return this.applicationPath + url.Substring(2);
                else if (url.StartsWith("~/"))
                    return this.applicationPath + url.Substring(1);
                else if (url.StartsWith("^/"))
                    return this.applicationPath + url.Substring(1);
            }
            return url;
        }
    }
}
